package com.lanou.renrenxin.tools;

import android.text.TextUtils;
import android.util.Log;

/**
 * Created by zy on 16/2/29.
 * 封装log
 * 输出大于等于LEVEL的日志,当LEVEL=NOTHING 时屏蔽所有日志
 * 一般用I就行,此封装类可简化
 */
public class LogUtils {

    public static final int VERBOSE = 1;
    public static final int DEBUG = 2;
    public static final int INFO = 3;
    public static final int WARN = 4;
    public static final int ERROR = 5;
    public static final int NOTHING = 6;
    public static final int LEVEL = VERBOSE;
    public static final String SEPARATOR = ",";
    private static String defaultTAG;

    //message输出log内容,当等级小于VERBOSE时打印log
    public static void v(String message){
        if (LEVEL <= VERBOSE){
            //StackTraceElement可以得到方法的调用栈信息
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            String tag = getDefaultTAG(stackTraceElement);
            Log.v(tag,getLogInfo(stackTraceElement) + message);
        }
    }

    public static void v(String tag,String message){
        if (LEVEL <= VERBOSE) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            if (TextUtils.isEmpty(tag)) {
                tag = getDefaultTAG(stackTraceElement);
            }
            Log.v(tag, getLogInfo(stackTraceElement) + message);
        }

    }

    //小于DEBUG时打印
    public static void d(String message) {
        if (LEVEL <= DEBUG) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            String tag = getDefaultTAG(stackTraceElement);
            Log.d(tag, getLogInfo(stackTraceElement) + message);
        }
    }

    public static void d(String tag, String message) {
        if (LEVEL <= DEBUG) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            if (TextUtils.isEmpty(tag)) {
                tag = getDefaultTAG(stackTraceElement);
            }
            Log.d(tag, getLogInfo(stackTraceElement) + message);
        }
    }

    //小于INFO时打印
    public static void i(String message) {
        if (LEVEL <= INFO) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            String tag = getDefaultTAG(stackTraceElement);
            Log.i(tag, getLogInfo(stackTraceElement) + message);
        }
    }

    public static void i(String tag, String message) {
        if (LEVEL <= INFO) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            if (TextUtils.isEmpty(tag)) {
                tag = getDefaultTAG(stackTraceElement);
            }
            Log.i(tag, getLogInfo(stackTraceElement) + message);
        }
    }

    //小于WARN时打印
    public static void w(String message) {
        if (LEVEL <= WARN) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            String tag = getDefaultTAG(stackTraceElement);
            Log.w(tag, getLogInfo(stackTraceElement) + message);
        }
    }

    public static void w(String tag, String message) {
        if (LEVEL <= WARN) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            if (TextUtils.isEmpty(tag)) {
                tag = getDefaultTAG(stackTraceElement);
            }
            Log.w(tag, getLogInfo(stackTraceElement) + message);
        }
    }

    //小于ERROR打印n
    public static void e(String tag, String message) {
        if (LEVEL <= ERROR) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
            if (TextUtils.isEmpty(tag)) {
                tag = getDefaultTAG(stackTraceElement);
            }
            Log.e(tag, getLogInfo(stackTraceElement) + message);
        }
    }

    //获取默认的TAG名称
    public static String getDefaultTAG(StackTraceElement stackTraceElement) {
        String fileName = stackTraceElement.getFileName();
        String stringArray[] = fileName.split("\\.");
        String tag = stringArray[0];
        return tag;
    }
    //输出日志所包含的信息
    public static String getLogInfo(StackTraceElement stackTraceElement){
        //StringBuilder修改字符串而不创建新的对象
        StringBuilder logInfoStringBuilder = new StringBuilder();
        //获取线程名
        String threadName = Thread.currentThread().getName();
        //获取文件名
        String fileName = stackTraceElement.getFileName();
        //获取类名,包名
        String className = stackTraceElement.getClassName();
        //获取方法名称
        String methodName = stackTraceElement.getMethodName();

        //输出
        logInfoStringBuilder.append("[ ");
        logInfoStringBuilder.append("threadName=" + threadName).append(SEPARATOR);
        logInfoStringBuilder.append("fileName=" + fileName).append(SEPARATOR);
        logInfoStringBuilder.append("className=" + className).append(SEPARATOR);
        logInfoStringBuilder.append("methodName=" + methodName).append(SEPARATOR);
        logInfoStringBuilder.append(" ] ");
        return logInfoStringBuilder.toString();
    }
}
